package com.yuda.campsite.controller;

import com.yuda.campsite.domain.*;
import com.yuda.campsite.service.ICampsiteOrderService;
import com.yuda.campsite.service.JingXiaoShangFromDbService;
import com.yuda.campsite.service.NumberCountRiQiService;
import com.yuda.campsite.service.NumberCountShengFenService;
import com.yuda.common.core.utils.poi.ExcelUtil;
import com.yuda.common.core.web.domain.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: xiaohudie
 * @Date: 2021/12/30 16:41
 * @Version: 1.8
 */
@RestController
@RequestMapping("/NumberOfRegistered")
public class NumberOfRegisteredController {
    @Resource
    private ICampsiteOrderService campsiteOrderService ;
    @Resource
    private NumberCountShengFenService numberCountShengFenService;
    @Resource
    private NumberCountRiQiService numberCountRiQiService;
    @Resource
    private JingXiaoShangFromDbService jingXiaoShangFromDbService;

    @GetMapping("/HuoDeShuJu")
    /**获得营地当前各个状态人数统计*/
    public AjaxResult HuoDeShuJu(@RequestParam(value = "campsiteId" ) String campsiteId){
        List<CampsiteOrder> campsiteOrders = campsiteOrderService.ChaKanNumOfStatus(campsiteId);
        Integer First =0;
        Integer Second =0;
        Integer Third =0;
        Integer Fourth =0;
        Integer Fifth =0;
        Integer Sixth =0;
        Integer Seventh =0;
        Integer Eighth =0;
        Integer Ninth =0;
        Integer Tenth =0;

        /**新建这个各个状态的展示对象，并且全部数据初始化为0*/
      NumberOfRegistered numberOfRegistered =new NumberOfRegistered();
      List<NumberOfRegistered> numberOfRegisteredList =new ArrayList<>();
        for (CampsiteOrder campsiteOrder : campsiteOrders) {
            if (campsiteOrder.getStatus()!=null && campsiteOrder.getStatus()==0){
                /**当他属于status等于0的时候，属于进入报名流程状态*/
                First++;
            }
            if (campsiteOrder.getStatus()!=null && campsiteOrder.getStatus()==1){
                /**当他属于status等于1的时候，属于已阅读报名须知状态*/
                Second++;
            }
            if (campsiteOrder.getStatus()!=null && campsiteOrder.getStatus()==2 && campsiteOrder.getInvitationCode() ==null ){
                /**当他属于status等于2而且邀请码为null的时候，属于未使用邀请码状态*/
                Third++;
            }
            if (campsiteOrder.getStatus()!=null && campsiteOrder.getStatus()==2 && campsiteOrder.getInvitationCode() !=null){
                /**当他属于status等于2而且邀请码不为null的时候，属于使用了邀请码状态*/
                Fourth++;
            }
            if (campsiteOrder.getStatus()!=null && campsiteOrder.getStatus()==3){
                /**当他属于status等于3的时候，属于已填写报名资料状态*/
               Fifth++;
            }
            if (campsiteOrder.getStatus()!=null && campsiteOrder.getStatus()==4 && campsiteOrder.getExamineStatus()!=null && campsiteOrder.getExamineStatus()==0){
                /**当他属于status等于4而且资料审核状态等于0的时候,属于已填写资料未审核状态*/
               Sixth++;
            }
            if (campsiteOrder.getStatus()!=null && campsiteOrder.getStatus()==4 && campsiteOrder.getExamineStatus()!=null && campsiteOrder.getExamineStatus()==1){
                /**当他属于status等于4，而且资料审核状态等于1的时候，属于资料审核通过状态*/
                Seventh++;
            }
            if (campsiteOrder.getStatus()!=null && campsiteOrder.getStatus()==4 && campsiteOrder.getExamineStatus()!=null && campsiteOrder.getExamineStatus()==2){
                /**当他属于status等于4，而且资料审核状态等于2的时候，属于资料审核未通过状态*/
                Eighth++;
            }
            if (campsiteOrder.getStatus()!=null &&campsiteOrder.getStatus()==5){
                /**当他属于status等于5的时候，属于待支付状态*/
                Ninth++;
            }
            if (campsiteOrder.getStatus()!=null && campsiteOrder.getStatus()==6){
                /**当他属于status等于6的时候，属于报名成功状态*/
                Tenth++;
            }

        }
        /**将得到的数据进行对象赋值*/
        numberOfRegistered.setNumberFirst(First);
        numberOfRegistered.setNumberSecond(Second);
        numberOfRegistered.setNumberThird(Third);
        numberOfRegistered.setNumberFourth(Fourth);
        numberOfRegistered.setNumberFifth(Fifth);
        numberOfRegistered.setNumberSixth(Sixth);
        numberOfRegistered.setNumberSeventh(Seventh);
        numberOfRegistered.setNumberEighth(Eighth);
        numberOfRegistered.setNumberNinth(Ninth);
        numberOfRegistered.setNumberTenth(Tenth);
        numberOfRegisteredList.add(numberOfRegistered);
        return AjaxResult.success("查询成功,数据为(与表格意义对应):",numberOfRegisteredList);

    }

  /**获取营地30天之内报名成功数量*/
  @GetMapping("/HuoDeBaoMingSuccess")
  public AjaxResult HuoDeBaoMingSuccess(@RequestParam("campsiteId") String campsiteId){
      List<NumberCountRiQi> numberCountRiQis = numberCountRiQiService.NumberCountRiQi(campsiteId);
      return AjaxResult.success("查询成功:", numberCountRiQis);
  }
  /**获取各个省份报名人数统计*/
  @GetMapping("/ShengFenShuJu")
    public AjaxResult ShengFenShuJu(@RequestParam("campsiteId") String campsiteId){
      List<NumberCountShengFen> numberCountShengFens = numberCountShengFenService.NumberCount(campsiteId);
      return AjaxResult.success("查询成功:",numberCountShengFens);
  }
  /**获取经销商的各个报名人数*/
  @GetMapping("/JingXiaoShang")
    public AjaxResult JingXiaoShang(@RequestParam("campsiteId") String campsiteId) {
      /**首先获得所有通过经销商进行报名的学生对象*/
      List<JingXiaoShangFromDb> jingXiaoShangFromDbs = jingXiaoShangFromDbService.JingXiaoShangFromDb(campsiteId);
      /**将所查到的数据根据经销商名称分类*/
      Set<String> stringSet = new HashSet<>();
      for (JingXiaoShangFromDb jingXiaoShangFromDb : jingXiaoShangFromDbs) {
          stringSet.add(jingXiaoShangFromDb.getName());
      }
      System.out.println("******" +stringSet);
      /**此时这个set集合中放的就是符合要求的所有经销商名称 而且不重复
       * 然后根据这个名称进行计数，将得到的值赋给一个新的对象*/


      /**新建这个各个状态的展示对象，并且全部数据初始化为0*/

      List<NumberOfRegistered> numberOfRegisteredList = new ArrayList<>();
      for (String s : stringSet) {
          Integer First = 0;
          Integer Second = 0;
          Integer Third = 0;
          Integer Fourth = 0;
          Integer Fifth = 0;
          Integer Sixth = 0;
          Integer Seventh = 0;
          Integer Eighth = 0;
          Integer Ninth = 0;
          Integer Tenth = 0;
          NumberOfRegistered numberOfRegistered = new NumberOfRegistered();
          for (JingXiaoShangFromDb jingXiaoShangFromDb : jingXiaoShangFromDbs) {
              if (jingXiaoShangFromDb.getName()!=null && jingXiaoShangFromDb.getName().equals(s) && jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 0) {
                  /**当他属于status等于0的时候，属于进入报名流程状态*/
                  First++;
              }
              if (jingXiaoShangFromDb.getName()!=null && jingXiaoShangFromDb.getName().equals(s) && jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 1) {
                  /**当他属于status等于1的时候，属于已阅读报名须知状态*/
                  Second++;
              }
              if (jingXiaoShangFromDb.getName()!=null && jingXiaoShangFromDb.getName().equals(s) && jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 2 && jingXiaoShangFromDb.getInvitationCode() == null) {
                  /**当他属于status等于2而且邀请码为null的时候，属于未使用邀请码状态*/
                  Third++;
              }
              if (jingXiaoShangFromDb.getName()!=null && jingXiaoShangFromDb.getName().equals(s) && jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 2 && jingXiaoShangFromDb.getInvitationCode() != null) {
                  /**当他属于status等于2而且邀请码不为null的时候，属于使用了邀请码状态*/
                  Fourth++;
              }
              if (jingXiaoShangFromDb.getName()!=null && jingXiaoShangFromDb.getName().equals(s) && jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 3) {
                  /**当他属于status等于3的时候，属于已填写报名资料状态*/
                  Fifth++;
              }
              if (jingXiaoShangFromDb.getName()!=null && jingXiaoShangFromDb.getName().equals(s) && jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 4 && jingXiaoShangFromDb.getExamineStatus() != null && jingXiaoShangFromDb.getExamineStatus() == 0) {
                  /**当他属于status等于4而且资料审核状态等于0的时候,属于已填写资料未审核状态*/
                  Sixth++;
              }
              if (jingXiaoShangFromDb.getName()!=null && jingXiaoShangFromDb.getName().equals(s) && jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 4 && jingXiaoShangFromDb.getExamineStatus() != null && jingXiaoShangFromDb.getExamineStatus() == 1) {
                  /**当他属于status等于4，而且资料审核状态等于1的时候，属于资料审核通过状态*/
                  Seventh++;
              }
              if (jingXiaoShangFromDb.getName()!=null && jingXiaoShangFromDb.getName().equals(s) && jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 4 && jingXiaoShangFromDb.getExamineStatus() != null && jingXiaoShangFromDb.getExamineStatus() == 2) {
                  /**当他属于status等于4，而且资料审核状态等于2的时候，属于资料审核未通过状态*/
                  Eighth++;
              }
              if (jingXiaoShangFromDb.getName()!=null && jingXiaoShangFromDb.getName().equals(s) && jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 5) {
                  /**当他属于status等于5的时候，属于待支付状态*/
                  Ninth++;
              }
              if (jingXiaoShangFromDb.getName()!=null && jingXiaoShangFromDb.getName().equals(s) && jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 6) {
                  /**当他属于status等于6的时候，属于报名成功状态*/
                  Tenth++;
              }
          }
          /**将得到的数据进行对象赋值*/
          numberOfRegistered.setJingXiaoShangName(s);
          numberOfRegistered.setNumberFirst(First);
          numberOfRegistered.setNumberSecond(Second);
          numberOfRegistered.setNumberThird(Third);
          numberOfRegistered.setNumberFourth(Fourth);
          numberOfRegistered.setNumberFifth(Fifth);
          numberOfRegistered.setNumberSixth(Sixth);
          numberOfRegistered.setNumberSeventh(Seventh);
          numberOfRegistered.setNumberEighth(Eighth);
          numberOfRegistered.setNumberNinth(Ninth);
          numberOfRegistered.setNumberTenth(Tenth);
          numberOfRegistered.setJingXiaoShangName(s);
          numberOfRegisteredList.add(numberOfRegistered);
          System.out.println("&&&&&&&&&"+numberOfRegisteredList);

      }

  return AjaxResult.success("查询成功:" ,numberOfRegisteredList);
  }
  @PostMapping("/ExportJinXiaoShang")
    public void ExportJinXiaoShang(HttpServletResponse response, @RequestParam(value = "campsiteId") String campsiteId,
                                   @RequestParam(value = "name") String name) throws IOException {
      List<JingXiaoShangFromDb> jingXiaoShangFromDbs = jingXiaoShangFromDbService.JingXiaoShangDaoBiao(campsiteId, name);
      Integer First =0;
      Integer Second =0;
      Integer Third =0;
      Integer Fourth =0;
      Integer Fifth =0;
      Integer Sixth =0;
      Integer Seventh =0;
      Integer Eighth =0;
      Integer Ninth =0;
      Integer Tenth =0;

      /**新建这个各个状态的展示对象，并且全部数据初始化为0*/

      for (JingXiaoShangFromDb jingXiaoShangFromDb : jingXiaoShangFromDbs) {
          if (jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 0) {
              /**当他属于status等于0的时候，属于进入报名流程状态*/
              First++;
          }
          if (jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 1) {
              /**当他属于status等于1的时候，属于已阅读报名须知状态*/
              Second++;
          }
          if (jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 2 && jingXiaoShangFromDb.getInvitationCode() == null) {
              /**当他属于status等于2而且邀请码为null的时候，属于未使用邀请码状态*/
              Third++;
          }
          if (jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 2 && jingXiaoShangFromDb.getInvitationCode() != null) {
              /**当他属于status等于2而且邀请码不为null的时候，属于使用了邀请码状态*/
              Fourth++;
          }
          if (jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 3) {
              /**当他属于status等于3的时候，属于已填写报名资料状态*/
              Fifth++;
          }
          if (jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 4 && jingXiaoShangFromDb.getExamineStatus() != null && jingXiaoShangFromDb.getExamineStatus() == 0) {
              /**当他属于status等于4而且资料审核状态等于0的时候,属于已填写资料未审核状态*/
              Sixth++;
          }
          if (jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 4 && jingXiaoShangFromDb.getExamineStatus() != null && jingXiaoShangFromDb.getExamineStatus() == 1) {
              /**当他属于status等于4，而且资料审核状态等于1的时候，属于资料审核通过状态*/
              Seventh++;
          }
          if (jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 4 && jingXiaoShangFromDb.getExamineStatus() != null && jingXiaoShangFromDb.getExamineStatus() == 2) {
              /**当他属于status等于4，而且资料审核状态等于2的时候，属于资料审核未通过状态*/
              Eighth++;
          }
          if (jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 5) {
              /**当他属于status等于5的时候，属于待支付状态*/
              Ninth++;
          }
          if ( jingXiaoShangFromDb.getStatus() != null && jingXiaoShangFromDb.getStatus() == 6) {
              /**当他属于status等于6的时候，属于报名成功状态*/
              Tenth++;
          }

      }
      ExportJinXiaoShang exportJinXiaoShang =new ExportJinXiaoShang();
      exportJinXiaoShang.setJinXiaoShangName(name);
      exportJinXiaoShang.setStatus("进入报名流程");
      exportJinXiaoShang.setNumber(First);
      ExportJinXiaoShang exportJinXiaoShang2 =new ExportJinXiaoShang();
      exportJinXiaoShang2.setJinXiaoShangName(name);
      exportJinXiaoShang2.setStatus("已阅读报名须知");
      exportJinXiaoShang2.setNumber(Second);
      ExportJinXiaoShang exportJinXiaoShang3 =new ExportJinXiaoShang();
      exportJinXiaoShang3.setJinXiaoShangName(name);
      exportJinXiaoShang3.setStatus("未使用邀请码");
      exportJinXiaoShang3.setNumber(Third);
      ExportJinXiaoShang exportJinXiaoShang4 =new ExportJinXiaoShang();
      exportJinXiaoShang4.setJinXiaoShangName(name);
      exportJinXiaoShang4.setStatus("已使用邀请码");
      exportJinXiaoShang4.setNumber(Fourth);
      ExportJinXiaoShang exportJinXiaoShang5 =new ExportJinXiaoShang();
      exportJinXiaoShang5.setJinXiaoShangName(name);
      exportJinXiaoShang5.setStatus("已填写报名资料");
      exportJinXiaoShang5.setNumber(Fifth);
      ExportJinXiaoShang exportJinXiaoShang6 =new ExportJinXiaoShang();
      exportJinXiaoShang6.setJinXiaoShangName(name);
      exportJinXiaoShang6.setStatus("已提交审核资料(未审核)");
      exportJinXiaoShang6.setNumber(Sixth);
      ExportJinXiaoShang exportJinXiaoShang7 =new ExportJinXiaoShang();
      exportJinXiaoShang7.setJinXiaoShangName(name);
      exportJinXiaoShang7.setStatus("已提交审核资料(审核通过)");
      exportJinXiaoShang7.setNumber(Seventh);
      ExportJinXiaoShang exportJinXiaoShang8 =new ExportJinXiaoShang();
      exportJinXiaoShang8.setJinXiaoShangName(name);
      exportJinXiaoShang8.setStatus("已提交审核资料(审核未通过)");
      exportJinXiaoShang8.setNumber(Eighth);
      ExportJinXiaoShang exportJinXiaoShang9 =new ExportJinXiaoShang();
      exportJinXiaoShang9.setJinXiaoShangName(name);
      exportJinXiaoShang9.setStatus("待支付");
      exportJinXiaoShang9.setNumber(Ninth);
      ExportJinXiaoShang exportJinXiaoShang10 =new ExportJinXiaoShang();
      exportJinXiaoShang10.setJinXiaoShangName(name);
      exportJinXiaoShang10.setStatus("报名成功");
      exportJinXiaoShang10.setNumber(Tenth);
     List<ExportJinXiaoShang> exportJinXiaoShangList =new ArrayList<>();
      exportJinXiaoShangList.add(exportJinXiaoShang);
      exportJinXiaoShangList.add(exportJinXiaoShang2);
      exportJinXiaoShangList.add(exportJinXiaoShang3);
      exportJinXiaoShangList.add(exportJinXiaoShang4);
      exportJinXiaoShangList.add(exportJinXiaoShang5);
      exportJinXiaoShangList.add(exportJinXiaoShang6);
      exportJinXiaoShangList.add(exportJinXiaoShang7);
      exportJinXiaoShangList.add(exportJinXiaoShang8);
      exportJinXiaoShangList.add(exportJinXiaoShang9);
      exportJinXiaoShangList.add(exportJinXiaoShang10);
      ExcelUtil<ExportJinXiaoShang> util = new ExcelUtil<ExportJinXiaoShang>(ExportJinXiaoShang.class);
      util.exportExcel(response, exportJinXiaoShangList, "conf");
  }
  @GetMapping("/NewQiSi")
    public AjaxResult NewQiSi(@RequestParam("campsiteId") String campsiteId) throws ParseException {
      String s = numberCountRiQiService.ChaXunSubDate(campsiteId);
      String  a=s+" 00:00:00";
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//给定的时间
      Date d=format.parse(a);

//当前时间处理
      Calendar cal = Calendar.getInstance();
      cal.set(Calendar.HOUR_OF_DAY, 0);
      cal.set(Calendar.MINUTE, 0);
      cal.set(Calendar.SECOND, 0);
      cal.set(Calendar.MILLISECOND, 0);
//给定时间处理
      Calendar setCal = Calendar.getInstance();
      setCal.setTime(d);
      setCal.set(Calendar.HOUR_OF_DAY, 0);
      setCal.set(Calendar.MINUTE, 0);
      setCal.set(Calendar.SECOND, 0);
      setCal.set(Calendar.MILLISECOND, 0);
      long dayDiff =(cal.getTimeInMillis()-setCal.getTimeInMillis())/(1000*60*60*24);
      /*long dayDiff2 =dayDiff+1;*/
      String[] strings =new String[(int) dayDiff];

      List<NumberCountRiQi> numberCountRiQis = numberCountRiQiService.NewQiSi(campsiteId,strings, s);
      return AjaxResult.success("查询成功:",numberCountRiQis);
  }
}

